package bb.lanxing.model.database;

import android.os.Parcel;
import android.os.Parcelable;
//import com.orm.SugarRecord;
//import com.orm.dsl.Table;
import com.orm.query.Select;
import com.tencent.open.SocialConstants;
import bb.lanxing.util.JsonUtil;
import bb.lanxing.util.Log;
import bb.lanxing.util.text.TextUtils;
import java.util.LinkedHashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

public class Medal extends LitePalTableBase implements Parcelable, Comparable<Medal> {
    public static final Parcelable.Creator<Medal> CREATOR = new Parcelable.Creator<Medal>() {

        public Medal createFromParcel(Parcel parcel) {
            return new Medal(parcel);
        }


        public Medal[] newArray(int i) {
            return new Medal[i];
        }
    };
    public static final int MEDAL_LEVEL_0 = 0;
    public static final int MEDAL_LEVEL_1 = 1;
    public static final int MEDAL_LEVEL_2 = 2;
    public static final int MEDAL_LEVEL_3 = 3;
    public static final int MEDAL_LEVEL_VALUE_1 = 100;
    public static final int MEDAL_LEVEL_VALUE_2 = 300;
    public static final int MEDAL_LEVEL_VALUE_3 = 1000;
    private String defaultPic;
    private String desc;
    private String detail;
    private long endTime;
    private String eventUrl;
    private String expiredPic;
    private int ismm;
    private long mid;
    private String mmdesc;
    private String mmtitle;
    private String pic;
    private long startTime;
    private String title;
    private int type;
    private String unactivedPic;
    private long userId;

    public static int compare(long j, long j2) {
        int i = (j > j2 ? 1 : (j == j2 ? 0 : -1));
        if (i < 0) {
            return -1;
        }
        return i == 0 ? 0 : 1;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public Medal() {
    }

    public Medal(JSONObject jSONObject) {
        setMid(JsonUtil.getLongValue("mid", jSONObject));
        setTitle(JsonUtil.getStringValue("mtitle", jSONObject));
        setDesc(JsonUtil.getStringValue("mdesc", jSONObject));
        setStartTime(JsonUtil.getLongValue("mstime", jSONObject));
        setEndTime(JsonUtil.getLongValue("metime", jSONObject));
        setType(JsonUtil.getIntegerValue("mptype", jSONObject));
        setDefaultPic(JsonUtil.getStringValue("mdpic", jSONObject));
        if (jSONObject.has("minfo")) {
            setDetail(JsonUtil.getArrayValue("minfo", jSONObject).toString());
        }
        try {
            if (jSONObject.has("mpic") && !TextUtils.isEmptyOrNull(jSONObject.getString("mpic"))) {
                setPic(JsonUtil.getStringValue("mpic", jSONObject));
            } else if (jSONObject.has("mmpic") && !TextUtils.isEmptyOrNull(jSONObject.getString("mmpic"))) {
                setPic(JsonUtil.getStringValue("mmpic", jSONObject));
            } else {
                setPic(JsonUtil.getStringValue("mdpic", jSONObject));
            }
        } catch (Exception e) {
            Log.e("Exc", e.getMessage());
        }
        setExpiredPic(JsonUtil.getStringValue("mexpired", jSONObject));
        setUnactivedPic(JsonUtil.getStringValue("munactived", jSONObject));
        setIsmm(JsonUtil.getIntegerValue("ismm", jSONObject));
        setMmdesc(JsonUtil.getStringValue("mmdesc", jSONObject));
        setMmtitle(JsonUtil.getStringValue("mmtitle", jSONObject));
        setEventUrl(JsonUtil.getStringValue("jumper", jSONObject));
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(this.detail);
        parcel.writeString(this.title);
        parcel.writeLong(this.startTime);
        parcel.writeLong(this.endTime);
        parcel.writeString(this.desc);
        parcel.writeLong(this.mid);
        parcel.writeString(this.defaultPic);
        parcel.writeInt(this.type);
        parcel.writeLong(this.userId);
        parcel.writeString(this.expiredPic);
        parcel.writeString(this.unactivedPic);
        parcel.writeString(this.pic);
        parcel.writeString(this.mmdesc);
        parcel.writeInt(this.ismm);
        parcel.writeString(this.mmtitle);
        parcel.writeString(this.eventUrl);
    }

    protected Medal(Parcel parcel) {
        this.detail = parcel.readString();
        this.title = parcel.readString();
        this.startTime = parcel.readLong();
        this.endTime = parcel.readLong();
        this.desc = parcel.readString();
        this.mid = parcel.readLong();
        this.defaultPic = parcel.readString();
        this.type = parcel.readInt();
        this.userId = parcel.readLong();
        this.expiredPic = parcel.readString();
        this.unactivedPic = parcel.readString();
        this.pic = parcel.readString();
        this.mmdesc = parcel.readString();
        this.ismm = parcel.readInt();
        this.mmtitle = parcel.readString();
        this.eventUrl = parcel.readString();
    }

    @Override
    public int compareTo(Medal medal) {
        return compare(medal.getStartTime(), this.startTime);
    }

    public boolean equals(Object obj) {
        return (obj instanceof Medal) && this.startTime == ((Medal) obj).getStartTime();
    }

    public static List<Medal> getAll() {

        List<Medal> l = LitePal.order("start_time desc").find(Medal.class);
        return l;

//        return Select.from(Medal.class).orderBy("start_time desc").list();
    }

    public static Medal getById(long j) {
        String id = String.valueOf(j);

        Medal m = LitePal.where("baseObjId=?", id).findFirst(Medal.class);
        return m;

//        return (Medal) Select.from(Medal.class).where(" id = ?", new String[]{String.valueOf(j)}).first();
    }

    public static LinkedHashMap<String, String> getDetailMap(String str) {
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<>();
        try {
            JSONArray jSONArray = new JSONArray(str);
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject jSONObject = jSONArray.getJSONObject(i);
                linkedHashMap.put(JsonUtil.getStringValue("url", jSONObject), JsonUtil.getStringValue(SocialConstants.PARAM_APP_DESC, jSONObject));
            }
            if (!linkedHashMap.isEmpty()) {
                return linkedHashMap;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



    public void setMid(long j) {
        this.mid = j;
    }

    public long getMid() {
        return this.mid;
    }

    public String getExpiredPic() {
        return this.expiredPic;
    }

    public void setExpiredPic(String str) {
        this.expiredPic = str;
    }

    public String getUnactivedPic() {
        return this.unactivedPic;
    }

    public void setUnactivedPic(String str) {
        this.unactivedPic = str;
    }

    public long getUserId() {
        return this.userId;
    }

    public void setUserId(long j) {
        this.userId = j;
    }

    public String getDetail() {
        return this.detail;
    }

    public void setDetail(String str) {
        this.detail = str;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String str) {
        this.title = str;
    }

    public long getStartTime() {
        return this.startTime;
    }

    public void setStartTime(long j) {
        this.startTime = j;
    }

    public long getEndTime() {
        return this.endTime;
    }

    public void setEndTime(long j) {
        this.endTime = j;
    }

    public String getDesc() {
        return this.desc;
    }

    public void setDesc(String str) {
        this.desc = str;
    }

    public String getPic() {
        return this.pic;
    }

    public void setPic(String str) {
        this.pic = str;
    }

    public String getDefaultPic() {
        return this.defaultPic;
    }

    public void setDefaultPic(String str) {
        this.defaultPic = str;
    }

    public int getType() {
        return this.type;
    }

    public void setType(int i) {
        this.type = i;
    }

    public String getMmdesc() {
        return this.mmdesc;
    }

    public int getIsmm() {
        return this.ismm;
    }

    public void setMmdesc(String str) {
        this.mmdesc = str;
    }

    public void setIsmm(int i) {
        this.ismm = i;
    }

    public void setMmtitle(String str) {
        this.mmtitle = str;
    }

    public String getMmtitle() {
        return this.mmtitle;
    }

    public String getEventUrl() {
        return this.eventUrl;
    }

    public void setEventUrl(String str) {
        this.eventUrl = str;
    }


}
